/*
 *  $Id: HeroicMonument.cs 975 2009-12-16 17:13:22Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

namespace Ragima.Cards001.Light.Lands {
    public class HeroicMonument: LandCard<GraveyardData> {
        public HeroicMonument(): base("001_l_l_heroic_monument") {
            Level = CompleteLevel.ReadyForTest;
            Element = ElementType.Light;
            Value = 100;
            Target = GraveyardTarget.Sacrifice;
        }

        public override void SetNaturalAffects(ICollector<Affect> affects) {
            base.SetNaturalAffects(affects);
            affects.Add(new Affects.ManaIncome(ElementType.Light, 1));
            affects.Add<HeroicMonumentAura>();
        }

        protected override void DoAction(TargetArgs<GraveyardData> args) {
            args.Target.MoveToSecondDeck();
            base.DoAction(args);
        }

        public static int GetHeroicMonumentCount(Player player) {
            int count = 0;
            foreach (Land land in player.Lands)
                if (land.Card is HeroicMonument)
                    count++;
            return count;
        }
        public static Land GetHeroicMonument(Player player) {
            foreach (Land land in player.Lands)
                if (land.Card is HeroicMonument)
                    return land;
            return null;
        }

        private class HeroicMonumentAura: CreatureAuraAffect {
            public HeroicMonumentAura(): base(true, false) {}

            public override string Id {
                get { return AffectManager.GetCardId<HeroicMonument>("aura"); }
            }

            protected override Affect CreateSubAffect() {
                return new HeroicMonumentAffect();
            }
        }

        private class HeroicMonumentAffect: ActivatableAffect {
            private static readonly Languages.CardPhrase<HeroicMonument> Action = new Languages.CardPhrase<HeroicMonument>("action");
            public override string Id {
                get { return AffectManager.GetCardId<HeroicMonument>("aura"); }
            }

            public override void Activate() {
                base.Activate();
                LocalSummary.OnStartOffencePhase += DoOffence;
            }

            public override void Deactivate() {
                base.Deactivate();
                LocalSummary.OnStartOffencePhase -= DoOffence;
            }

            private void DoOffence() {
                int count = GetHeroicMonumentCount(Owner.Owner);
                if (count == 0) return;
                if (count < 6) {
                    int dice = Game.Current.GetDiceNextValue();
                    if (dice < (7-count)) return;
                }
                Game.Current.Log.Add(Action.Phrase, Owner, GetHeroicMonument(Owner.Owner));
                Owner.AddAffect<HeroicMonumentFearless>();
            }
        }

        private class HeroicMonumentFearless: SpellAffect<HeroicMonument> {
            public HeroicMonumentFearless(): base(true) {
                DefaultDescription = true;
                TimeAffect time = new TimeAffect(2);
                time.Add(new Affects.Tireless());
                time.Add(new Affects.Fearless());
                Add(time);
            }
        }
    }
}
